//
//  Hear_EditVoiceViewController.m
//  Hear
//
//  Created by mac on 2018/4/26.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "Hear_EditVoiceViewController.h"

#import "Hear_AddSoundViewController.h" // 添加背景音乐
#import "Hear_AddPhotoViewController.h" // 添加配图

#import "Hear_PreviewViewController.h" // 预览

@interface Hear_EditVoiceViewController ()

@property (nonatomic, strong) UIImageView *selectmusic;
@property (nonatomic, strong) UIImageView *defaultmusic;

/** 默认配图 */
@property (nonatomic, strong) UIImageView *defaultImg;
/** 配图选择 */
@property (nonatomic, strong) UIImageView *selectImg;
/** 添加配图显示文字 */
@property (nonatomic, strong) UILabel *addphoto;

/** 合成音本地路径 */
@property (nonatomic, strong) NSString *soundLocalStr;

/** 合成音时长 */
@property (nonatomic, assign) CGFloat SoundLength;

/** 背景图片json字符串 */
@property (nonatomic, copy) NSString *imageJson;

/** 音乐选择位置 */
@property (nonatomic, assign) NSInteger musicindex;
/** 图片选择位置 */
@property (nonatomic, assign) NSInteger index;

@end

@implementation Hear_EditVoiceViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    self.index = 1;
    self.musicindex = 1;
}

#pragma mark 界面
- (void)setupUI {
    self.title = @"编辑作品";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *previewbtn = [[UIButton alloc]init];
    previewbtn.size = CGSizeMake(30, 25);
    [previewbtn setTitle:@"预览" forState:UIControlStateNormal];
    previewbtn.titleLabel.font = CustomUIFont(14);
    [previewbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [previewbtn addTarget:self action:@selector(previewbtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:previewbtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    UIView *view1 = [UIView new];
    view1.backgroundColor = [UIColor colorWithHexString:@"#F1F1F1"];
    [self.view addSubview:view1];
    
    UILabel *music = [UILabel new];
    music.textAlignment = NSTextAlignmentLeft;
    music.textColor = [UIColor colorWithHexString:@"#6E6E6E"];
    music.font = CustomUIFont(15);
    music.text = @"留声机背景音乐";
    [view1 addSubview:music];
    
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.mas_equalTo(self.view);
        make.height.mas_equalTo(50 * scaleHeight);
    }];
    
    [music mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view1);
        make.leading.mas_equalTo(view1.mas_leading).mas_offset(20 * scaleWidth);
    }];
    
    UIView *view2 = [UIView new];
    view2.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
    [self.view addSubview:view2];
    
    UIImageView *selectmusic = [[UIImageView alloc] initWithImage:GetImgWithStr(@"edit_选中")];
    selectmusic.userInteractionEnabled = YES;
    self.selectmusic = selectmusic;
    [view2 addSubview:selectmusic];
    
    UILabel *addmusic = [UILabel new];
    addmusic.textAlignment = NSTextAlignmentLeft;
    addmusic.textColor = [UIColor colorWithHexString:@"#222222"];
    addmusic.font = CustomUIFont(14);
    addmusic.text = @"为该留声机添加背景音乐";
    [view2 addSubview:addmusic];
    
    UIButton *musicbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [musicbtn setImage:GetImgWithStr(@"edit_编辑") forState:UIControlStateNormal];
    [musicbtn setContentMode:UIViewContentModeScaleAspectFit];
    [musicbtn addTarget:self action:@selector(musicbtnAction) forControlEvents:UIControlEventTouchUpInside];
    [view2 addSubview:musicbtn];
    
    UIView *linem = [UIView new];
    linem.backgroundColor = [UIColor colorWithHexString:@"#E6E6E6"];
    [view2 addSubview:linem];
    [linem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(view2);
        make.height.mas_equalTo(1 * scaleHeight);
    }];
    
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.view);
        make.top.mas_equalTo(view1.mas_bottom);
        make.height.mas_equalTo(60 * scaleHeight);
    }];
    [selectmusic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view2);
        make.leading.mas_equalTo(view2.mas_leading).mas_offset(20 * scaleWidth);
    }];
    [addmusic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view2);
        make.leading.mas_equalTo(selectmusic.mas_trailing).mas_offset(18 * scaleWidth);
    }];
    [musicbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view2);
        make.trailing.mas_equalTo(view2.mas_trailing).mas_offset(-20 * scaleWidth);
        make.size.mas_equalTo(CGSizeMake(36 * scaleWidth, 36 * scaleHeight));
    }];
    
    UITapGestureRecognizer *addtap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addMusicTap)];
    [selectmusic addGestureRecognizer:addtap];
    [view2 addGestureRecognizer:addtap];
    
    UIView *view3 = [UIView new];
    view3.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
    [self.view addSubview:view3];
    
    UIImageView *defaultmusic = [[UIImageView alloc] initWithImage:GetImgWithStr(@"edit_默认")];
    defaultmusic.userInteractionEnabled = YES;
    self.defaultmusic = defaultmusic;
    [view3 addSubview:defaultmusic];
    
    UILabel *defaultmu = [UILabel new];
    defaultmu.textAlignment = NSTextAlignmentLeft;
    defaultmu.textColor = [UIColor colorWithHexString:@"#222222"];
    defaultmu.font = CustomUIFont(14);
    defaultmu.text = @"不使用官方背景音乐";
    [view3 addSubview:defaultmu];
    
    [view3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.view);
        make.top.mas_equalTo(view2.mas_bottom);
        make.height.mas_equalTo(60 * scaleHeight);
    }];
    [defaultmusic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view3);
        make.leading.mas_equalTo(view3.mas_leading).mas_offset(20 * scaleWidth);
    }];
    [defaultmu mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view3);
        make.leading.mas_equalTo(defaultmusic.mas_trailing).mas_offset(18 * scaleWidth);
    }];
    
    UIView *linemu = [UIView new];
    linemu.backgroundColor = [UIColor colorWithHexString:@"#E6E6E6"];
    [view3 addSubview:linemu];
    [linemu mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(view3);
        make.height.mas_equalTo(1 * scaleHeight);
    }];
    
    UITapGestureRecognizer *defaultMuTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(defaultMuTapAction)];
    [defaultmusic addGestureRecognizer:defaultMuTap];
    [view3 addGestureRecognizer:defaultMuTap];
    
    UIView *view4 = [UIView new];
    view4.backgroundColor = [UIColor colorWithHexString:@"#F1F1F1"];
    [self.view addSubview:view4];
    
    UILabel *photo = [UILabel new];
    photo.textAlignment = NSTextAlignmentLeft;
    photo.textColor = [UIColor colorWithHexString:@"#6E6E6E"];
    photo.font = CustomUIFont(15);
    photo.text = @"留声机配图";
    [view4 addSubview:photo];
    
    [view4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.view);
        make.top.mas_equalTo(view3.mas_bottom);
        make.height.mas_equalTo(50 * scaleHeight);
    }];
    
    [photo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view4);
        make.leading.mas_equalTo(view4.mas_leading).mas_offset(20 * scaleWidth);
    }];
    
    UIView *view5 = [UIView new];
    view5.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
    [self.view addSubview:view5];
    
    UIImageView *selectImg = [[UIImageView alloc] initWithImage:GetImgWithStr(@"edit_选中")];
    selectImg.userInteractionEnabled = YES;
    self.selectImg = selectImg;
    [view5 addSubview:selectImg];
    
    UILabel *addphoto = [UILabel new];
    addphoto.textAlignment = NSTextAlignmentLeft;
    addphoto.textColor = [UIColor colorWithHexString:@"#222222"];
    addphoto.font = CustomUIFont(14);
    addphoto.text = @"为该留声机添加配图 0/9";
    self.addphoto = addphoto;
    [view5 addSubview:addphoto];
    
    UIButton *photobtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [photobtn setImage:GetImgWithStr(@"edit_编辑") forState:UIControlStateNormal];
    [photobtn setContentMode:UIViewContentModeScaleAspectFit];
    [photobtn addTarget:self action:@selector(photobtnAction) forControlEvents:UIControlEventTouchUpInside];
    [view5 addSubview:photobtn];
    
    [view5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.view);
        make.top.mas_equalTo(view4.mas_bottom);
        make.height.mas_equalTo(60 * scaleHeight);
    }];
    [selectImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view5);
        make.leading.mas_equalTo(view5.mas_leading).mas_offset(20 * scaleWidth);
    }];
    [addphoto mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view5);
        make.leading.mas_equalTo(selectImg.mas_trailing).mas_offset(18 * scaleWidth);
    }];
    [photobtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view5);
        make.trailing.mas_equalTo(view5.mas_trailing).mas_offset(-20 * scaleWidth);
        make.size.mas_equalTo(CGSizeMake(36 * scaleWidth, 36 * scaleHeight));
    }];
    
    UIView *line1 = [UIView new];
    line1.backgroundColor = [UIColor colorWithHexString:@"#E6E6E6"];
    [view5 addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(view5);
        make.height.mas_equalTo(1 * scaleHeight);
    }];
    
    UITapGestureRecognizer *selectTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectTapAction)];
    [selectImg addGestureRecognizer:selectTap];
    [view5 addGestureRecognizer:selectTap];
    
    UIView *view6 = [UIView new];
    view6.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
    [self.view addSubview:view6];
    
    UIImageView *defaultImg = [[UIImageView alloc] initWithImage:GetImgWithStr(@"edit_默认")];
    defaultImg.userInteractionEnabled = YES;
    self.defaultImg = defaultImg;
    [view6 addSubview:defaultImg];
    
    UILabel *defaultlb = [UILabel new];
    defaultlb.textAlignment = NSTextAlignmentLeft;
    defaultlb.textColor = [UIColor colorWithHexString:@"#222222"];
    defaultlb.font = CustomUIFont(14);
    defaultlb.text = @"使用默认配图";
    [view6 addSubview:defaultlb];
    
    [view6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.view);
        make.top.mas_equalTo(view5.mas_bottom);
        make.height.mas_equalTo(60 * scaleHeight);
    }];
    [defaultImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view6);
        make.leading.mas_equalTo(view6.mas_leading).mas_offset(20 * scaleWidth);
    }];
    [defaultlb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view6);
        make.leading.mas_equalTo(defaultImg.mas_trailing).mas_offset(18 * scaleWidth);
    }];
    
    UIView *line = [UIView new];
    line.backgroundColor = [UIColor colorWithHexString:@"#E6E6E6"];
    [view6 addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(view6);
        make.height.mas_equalTo(1 * scaleHeight);
    }];
    
    UITapGestureRecognizer *defaultTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(defaultTapAction)];
    [defaultImg addGestureRecognizer:defaultTap];
    [view6 addGestureRecognizer:defaultTap];
    
}

#pragma mark 添加背景音乐
- (void)musicbtnAction {
    self.musicindex = 1;
    self.defaultmusic.image = GetImgWithStr(@"edit_默认");
    self.selectmusic.image = GetImgWithStr(@"edit_选中");
    
    if ([self.fromVC isEqualToString:@"answer"]) {
        // 回答话题
        Hear_AddSoundViewController *vc = [Hear_AddSoundViewController new];
        vc.soundPath = self.soundPath;
        vc.model = self.model;
        
        vc.localMusicBlock = ^(CGFloat SoundLength, NSString *SoundUrl) {
            self.soundLocalStr = SoundUrl;
            self.SoundLength = SoundLength;
        };
        
        [self.navigationController pushViewController:vc animated:YES];
        
    } else {
        
        // 创建话题
        Hear_AddSoundViewController *vc = [Hear_AddSoundViewController new];
        vc.soundPath = self.soundPath;
        
        vc.localMusicBlock = ^(CGFloat SoundLength, NSString *SoundUrl) {
            self.soundLocalStr = SoundUrl;
            self.SoundLength = SoundLength;
        };
        
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

#pragma mark 添加配图
- (void)photobtnAction {
    self.index = 1;
    self.defaultImg.image = GetImgWithStr(@"edit_默认");
    self.selectImg.image = GetImgWithStr(@"edit_选中");
    
    if ([self.fromVC isEqualToString:@"answer"]) {
        // 回答话题
        Hear_AddPhotoViewController *vc = [Hear_AddPhotoViewController new];
        vc.model = self.model;
        vc.getImageStr = ^(NSString *backgroundImage) {
            
            NSData *jsondata = [backgroundImage dataUsingEncoding:NSUTF8StringEncoding];
            
            NSArray *imageArr = [NSJSONSerialization JSONObjectWithData:jsondata options:NSJSONReadingAllowFragments | NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:nil];
            
            self.addphoto.text = [NSString stringWithFormat:@"为该留声机添加配图 %ld/9", imageArr.count];
            
            self.imageJson = backgroundImage;
            
        };
        
        [self.navigationController pushViewController:vc animated:YES];
        
    } else {
        // 创建话题
        Hear_AddPhotoViewController *vc = [Hear_AddPhotoViewController new];
        vc.getImageStr = ^(NSString *backgroundImage) {
            
            NSData *jsondata = [backgroundImage dataUsingEncoding:NSUTF8StringEncoding];
            
            NSArray *imageArr = [NSJSONSerialization JSONObjectWithData:jsondata options:NSJSONReadingAllowFragments | NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:nil];
            
            self.addphoto.text = [NSString stringWithFormat:@"为该留声机添加配图 %ld/9", imageArr.count];
            
            self.imageJson = backgroundImage;
            
        };
        
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

#pragma mark 预览
- (void)previewbtnAction:(UIButton *)sender {
    
    if (self.musicindex == 1 && !self.soundLocalStr.length) {
        [self showMessage:@"请选择背景音乐" atPosition:ShowPosition_Center];
        return;
    }
    
    if (self.index == 1 && !self.imageJson.length) {
        [self showMessage:@"请选择背景图" atPosition:ShowPosition_Center];
        return;
    }
    
    if (self.musicindex == 0) {
        self.soundLocalStr = self.soundPath;
        self.SoundLength = self.audioLength;
    }
    
    if ([self.fromVC isEqualToString:@"answer"]) {
        // 回答话题
        Hear_PreviewViewController *vc = [Hear_PreviewViewController new];
        vc.model = self.model;
        vc.soundStr = self.soundLocalStr;
        vc.soundLength = self.SoundLength;
        vc.imageJson = self.imageJson;
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        // 创建话题
        Hear_PreviewViewController *vc = [Hear_PreviewViewController new];
        vc.topicname = self.topicName;
        vc.soundStr = self.soundLocalStr;
        vc.soundLength = self.SoundLength;
        vc.imageJson = self.imageJson;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

#pragma mark 选择事件
- (void)addMusicTap {
    self.defaultmusic.image = GetImgWithStr(@"edit_默认");
    self.selectmusic.image = GetImgWithStr(@"edit_选中");
    
    self.musicindex = 1;
}

- (void)defaultMuTapAction {
    self.defaultmusic.image = GetImgWithStr(@"edit_选中");
    self.selectmusic.image = GetImgWithStr(@"edit_默认");
    
    self.musicindex = 0;
}

- (void)defaultTapAction {
    self.defaultImg.image = GetImgWithStr(@"edit_选中");
    self.selectImg.image = GetImgWithStr(@"edit_默认");
    
    self.index = 0;
}

- (void)selectTapAction {
    self.defaultImg.image = GetImgWithStr(@"edit_默认");
    self.selectImg.image = GetImgWithStr(@"edit_选中");
    
    self.index = 1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
